﻿using AutoMapper;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Write.API.application.Command.Permission;
using MediatR;

namespace BasicData.Write.API.application.CommandHandler.Permission
{
    public class AddCreatePermissionCommandHandler : IRequestHandler<AddCreatePermissionCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<PermissionModel> permissionRepo;
        private readonly IMapper mapper;
        public AddCreatePermissionCommandHandler(IBaseRepository<PermissionModel> permissionRepo, IMapper mapper)
        {
            this.permissionRepo = permissionRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<object>> Handle(AddCreatePermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> res = new ApiResult<object>();
            var per = mapper.Map<PermissionModel>(request);
            int r = permissionRepo.Create(per);
            if (r > 0)
            {
                res.Code = BaseEnums.Success;
                res.Msg = "权限信息添加成功";
                res.Data = r;
            }
            else
            {
                res.Code = BaseEnums.Failed;
                res.Msg = "权限信息添加失败";
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
